home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / AmiVoGL_MDEV.lha / hershey / src / h2v.c < prev    next >
C/C++ Source or Header  |  1994-12-18  |  4KB  |  214 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "hershey.h"
  6. #include "h2v.h"
  7.  
  8. #ifdef AZTEC_C
  9. # ifndef AMIGA
  10. #  define AMIGA
  11. # endif
  12. #endif
  13.  
  14. /* --------------------------------------------------------------------- */
  15.  
  16. /*
  17.  *    Read the raw hersh font data and write the binary font files
  18.  *    as specified in h2v.h or as specified in an index file.
  19.  */
  20.  
  21. HTAB    hersh[MAX_CHARS];
  22.  
  23. /* --------------------------------------------------------------------- */
  24.  
  25. /*
  26.  * main driver - if argc > 2 we are creating a file from an
  27.  * index, otherwise use the table in h2v.h
  28.  */
  29. int main(
  30.   int argc,
  31.   char **argv)
  32. {
  33.     FILE    *fp;
  34.     FTAB    table;
  35.     int    i;
  36.  
  37.     
  38.     if (argc != 2 && argc != 4) {
  39.         fprintf(stderr, "Usage: h2v datafile [indexfile fontfile]\n");
  40.         exit(1);
  41.     }
  42.     if ((fp = fopen(argv[1], "r")) == NULL) {
  43.         fprintf(stderr, "h2v: can't open hersh data file %s\n", argv[1]);
  44.         exit(1);
  45.     }
  46.  
  47.     readdata(fp);
  48.  
  49.     if (argc == 4) {
  50.         readindex(argv[2], argv[3], &table);
  51.         writefont(&table);
  52.     } else
  53.         for (i = 0; i < sizeof(fonts) / sizeof(FTAB); i++)
  54.             writefont(&fonts[i]);
  55.  
  56.     exit(0);
  57. }
  58.  
  59. /* --------------------------------------------------------------------- */
  60.  
  61. /*
  62.  *  readdata
  63.  *
  64.  *  Reads the raw hersh data
  65.  */
  66. void readdata(FILE *fp)
  67. {
  68.     int    i = 0, j, x, y;
  69.     int    charno, pairs;
  70.     char    buf[MAX_BUF];
  71.  
  72.     
  73.     while (getcharacter(fp, &charno, &pairs, buf)) {
  74.         hersh[charno - 1].ch = malloc(2 * pairs + 1);
  75.         strcpy(hersh[charno - 1].ch, buf);
  76.         hersh[charno - 1].len = strlen(hersh[charno - 1].ch);
  77.     }
  78.  
  79.     fclose(fp);
  80. }
  81.  
  82. /*
  83.  *  readindex
  84.  * 
  85.  *  Read an index file into index tab.
  86.  */
  87. void readindex(
  88.   char *name,
  89.   char *fname,
  90.   FTAB *tab)
  91. {
  92.     
  93.     FILE    *fp;
  94.     int    i;
  95.  
  96.  
  97.     if ((fp = fopen(name, "r")) == NULL) {
  98.         fprintf(stderr, "h2v: can't open index file\n");
  99.         exit(1);
  100.     }
  101.  
  102.     tab->name = fname;
  103.  
  104.     i = 0;
  105.     while (fscanf(fp, "%d %d", &tab->ent[i], &tab->ent[i + 1]) == 2)
  106.         if ((i += 2) >= MAX_ENTS - 2) {
  107.             fprintf(stderr, "h2v: indexfile too big - increase MAX_ENTS\n");
  108.             exit(1);
  109.         }
  110.     
  111.     tab->ent[i] = 0;
  112.  
  113.     fclose(fp);
  114. }
  115.  
  116. /*
  117.  * writefont
  118.  *
  119.  *    output a font to file name based on font table tab
  120.  */
  121. void writefont(FTAB *tab)
  122. {
  123.     int    l ,f, j, j1, x, y, k1, k2, k;
  124.     short    i, nchars, asdecw[3]; 
  125.     short    start, end, nvects, fd;
  126.     char    *p;
  127.     HTAB    *curch;
  128.  
  129.  
  130.     fprintf(stderr, "Font name: %s\n", tab->name);
  131.  
  132. #ifdef PC
  133.     if ((fd = open(tab->name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644)) < 0)
  134. #else
  135. # ifdef AMIGA
  136.     if ((fd = open(tab->name, O_WRONLY | O_CREAT | O_TRUNC)) < 0)
  137. # else
  138.     if ((fd = open(tab->name, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
  139. # endif
  140. #endif
  141.         {
  142.         fprintf(stderr, "Can't open output file: %s\n", tab->name);
  143.         exit(1); 
  144.         }
  145.  
  146.     asdecw[0] = asdecw[2] = -100;
  147.     asdecw[1] = 100;
  148.     nvects    = nchars    = 0;
  149.  
  150.     /*  Leave room for stuff at top */
  151. #ifdef AMIGA
  152.     /* finding this problem was not fun */
  153.     start= 0;
  154.     for(i= 0; i < 5; ++i) write(fd,&start,sizeof(short));
  155. #else
  156.     if(lseek(fd, (long)(5 * sizeof(short)), 0) < 0) {
  157.         perror("h2v's writefont: first lseek");
  158.         exit(1);
  159.         }
  160. #endif
  161.  
  162.     for (i = 0; (start = tab->ent[i]) != 0; i += 2) {
  163.         end = tab->ent[i + 1];
  164.         do {
  165.             curch = &hersh[start - 1];
  166.             nchars++;
  167.             if (curch->ch == (char *)NULL) {
  168.                 fprintf(stderr, "h2v: character %d not available\n", start);
  169.                 exit(1);
  170.             }
  171.             asdecw[2] = MAX(asdecw[2], curch->ch[1] - curch->ch[0]);
  172.  
  173.             for (p = &curch->ch[2]; *p; p++) {
  174.                 x = *p++;
  175.                 if (x != ' ') {
  176.                     asdecw[0] = MAX(asdecw[0], COORD(*p));
  177.                     asdecw[1] = MIN(asdecw[1], COORD(*p));
  178.                 }
  179.             }
  180.  
  181.             nvects += curch->len / 2;
  182.             if (write(fd, &curch->len, sizeof(short)) != sizeof(short)) {
  183.                 fprintf(stderr,"h2v: ERROR writing character length to file\n");
  184.                 exit(1);
  185.             }
  186.             if (write(fd, curch->ch, (unsigned)curch->len) != curch->len) {
  187.                 fprintf(stderr,"h2v: ERROR writing character data to file\n");
  188.                 exit(1);
  189.             }
  190.             start++;
  191.         } while (start <= end);
  192.     }
  193.  
  194.     if(lseek(fd, 0L, 0) < 0) {
  195.         perror("h2v's writefont: lseek to 0");
  196.         exit(1);
  197.         }
  198.     if (write(fd, &nchars, sizeof(nchars)) != sizeof(nchars)) {
  199.         fprintf(stderr,"Error writing to file\n");
  200.         exit(1);
  201.     }
  202.  
  203.     if (write(fd, &nvects, sizeof(nvects)) != sizeof(nvects)) {
  204.         fprintf(stderr,"Error writing to file\n");
  205.         exit(1);
  206.     }
  207.     if (write(fd, asdecw, sizeof(asdecw)) != sizeof(asdecw)) {
  208.         fprintf(stderr, "Error writing to file\n");
  209.         exit(1);
  210.     }
  211.  
  212.     close(fd);
  213. }
  214.